﻿/* Write a program that removes from a text file all words listed in given another text file. Handle all possible exceptions in your methods. */

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace _12.RemoveWordsListed
{
    class RemoveWordsListed
    {
        static void Main()
        {
            Console.Write("Path to text file: ");
            string path_to_input_file = Console.ReadLine();
            Console.Write("Path to text file which contains words to be removed: ");
            string path_to_words_file = Console.ReadLine();
            
            string fileContents;
            try
            {
                StreamReader SR = new StreamReader(@path_to_input_file);
                using (SR)
                {
                    fileContents = SR.ReadToEnd();
                }
            }
            catch (FileNotFoundException fn)
            {
                throw new FileNotFoundException("File not found, " + fn.Message);
            }
            catch (OverflowException oe)
            {
                throw new OverflowException("Stack overflow, may be the file is too big?, " + oe.Message);
            }
            catch (IOException ioe)
            {
                throw new IOException("Error: the input file could not be read, " + ioe.Message);
            }

            //read file with words to be removed and replace
            string line;
            try
            {
                StreamReader SR = new StreamReader(@path_to_words_file);
                line = SR.ReadLine();
                using (SR)
                {
                    while (line != null)
                    {
                        fileContents = fileContents.Replace(line, null);
                        line = SR.ReadLine();
                    }
                }
            }
            catch (FileNotFoundException fn)
            {
                throw new FileNotFoundException("File not found, " + fn.Message);
            }
            catch (OverflowException oe)
            {
                throw new OverflowException("Stack overflow, may be the file is too big?, " + oe.Message);
            }
            catch (IOException ioe)
            {
                throw new IOException("Error: the input file could not be read, " + ioe.Message);
            }

            try
            {
                StreamWriter SW = new StreamWriter(@path_to_input_file, false);
                using (SW)
                {
                    //remove blank lines
                    fileContents = Regex.Replace(fileContents, @"^\s*$", "", RegexOptions.Multiline);
                    //write new data to file
                    SW.Write(fileContents);
                }
            }
            catch (FileNotFoundException fn)
            {
                throw new FileNotFoundException("File not found, " + fn.Message);
            }
            catch (OverflowException oe)
            {
                throw new OverflowException("Stack overflow, may be the file is too big?, " + oe.Message);
            }
            catch (IOException ioe)
            {
                throw new IOException("Error: the input file could not be read, " + ioe.Message);
            }
            //remove empty spaces
            //Console.WriteLine("New text: {0}", fileContents);
            Console.WriteLine("Data succesfully written to {0} " , path_to_input_file);
        }
    }
}
